var controllerPlanId = null;
var oldInterruptStartDate = "";
var oldInterruptEndDate = "";
var offsetH = (heightClient)*0.02;
$(function () {
    layui.use(['table', 'form', 'laydate'], function () {
        var table = layui.table,
            form = layui.form,
            laydate = layui.laydate
        form.verify({
            war: [/^[+]{0,1}(\d+)$|^[+]{0,1}(\d+\.\d+)$/, '电量阈值必须是正数'],
            plus: [/^[0-9]*[1-9][0-9]*$/, '时间和次数必须是正整数']
        });
        laydate.render({
            elem: '#interruptStartDate',
            type: 'time'
        });
        laydate.render({
            elem: '#interruptEndDate',
            type: 'time'
        });
        var data = [
            {type: 'numbers', title: '序号',width:'3%'},
            {field: 'name', title: '方案名称', sort: false, align: 'center'},
            {field: 'currentThreshold', title: '限制电流(A)', sort: false, align: 'center', width: '7%'},
            {field: 'freeRoomHold', title: '闲置房间电量(KW/H)', sort: false, align: 'center', width: '12%'},
            {field: 'amountThreshold', title: '超电量阈值', sort: false, align: 'center', width: '7%'},
            {field: 'powerThreshold', title: '超功率阈值', sort: false, align: 'center', width: '7%'},
            {
                field: 'outageStatus', title: '欠费断电', sort: false, align: 'center', templet: function (d) {
                if (d.outageStatus == 1) return '<span class="font-red">是</span>'
                else if (d.outageStatus == 0) return '<span class="font-green">否</span>'
                else return ''
            }, width: '7%'
            },
            {field: 'interruptDate', title: '欠费断电时间', sort: false, align: 'center'},
            {field: 'errorThreshold', title: '错误上报次数', sort: false, align: 'center', width: '7%'},
            {field: 'warnOvertime', title: '超时间隔(min)', sort: false, align: 'center', width: '8%'},
            {field: 'warnBalance', title: '报警余额', sort: false, align: 'center', width: '7%'},
            {
                field: 'controllerStatus', title: '状态', sort: false, align: 'center', templet: function (d) {
                if (d.controllerStatus == 1) return '<span class="layui-badge layui-bg-green">可用</span>'
                else if (d.controllerStatus == 0) return '<span class="layui-badge layui-bg-cyan">禁用</span>'
                else return ''
            }, width: '5%'
            },
            {title: '操作', sort: false, align: 'center', toolbar: '#barDemo',fixed:'right',width:'10%'}
        ];
        table.render(renderTable("#controlList", 'controllerPlan/page', data))
        table.on('tool(controlList)', function (obj) {
            var data = obj.data;
            controllerPlanId = data.controllerPlanId;
            oldInterruptEndDate = data.interruptEndDate;
            oldInterruptStartDate = data.interruptStartDate;

            if (obj.event === "edit") {
                layer.open({
                    type: 1,
                    title: "编辑控制方案",
                    btnAlign: "c",
                    offset: [offsetH],
                    area: ['850px', '500px'],
                    content: $("#controlDiv"),
                    success: function () {
                        form.val('editForm', {
                            name: data.name,
                            expenseItemsName: data.expenseItemsName,
                            currentThreshold: data.currentThreshold,
                            freeRoomHold: data.freeRoomHold,
                            amountThreshold: data.amountThreshold,
                            powerThreshold: data.powerThreshold,
                            outageStatus: data.outageStatus,
                            interruptStartDate: data.interruptStartDate,
                            interruptEndDate: data.interruptEndDate,
                            errorThreshold: data.errorThreshold,
                            warnOvertime: data.warnOvertime,
                            warnBalance: data.warnBalance,
                            controllerStatus: data.controllerStatus
                        })
                    },
                    end: function (i) {
                        layer.close(i)
                        $("#editForm")[0].reset();
                        $("#controlDiv").hide();
                        controllerPlanId = null;
                        oldInterruptEndDate = '';
                        oldInterruptStartDate = '';
                    }
                });
            }
            if (obj.event === "del") {
                layer.confirm('确定要删除该电表控制方案？', function (index) {
                    postAjaxReq("controllerPlan/remove", {controllerPlanId: controllerPlanId}, function (res) {
                        if (res.code == 1) {
                            layer.msg("删除成功")
                            table.reload('controlList')
                        } else {
                            layer.msg(res.msg)
                        }
                    });
                    layer.close(index);
                    controllerPlanId = null
                })
            }
        });

        form.on('select(outageStatus)', function (data) {
            if(data.value == 0){
                $("#interruptStartDate").val("");
                $("#interruptEndDate").val("");
            }else {
                $("#interruptStartDate").val(oldInterruptStartDate);
                $("#interruptEndDate").val(oldInterruptEndDate);
            }
        });

        $("#addControl").click(function () {
            layer.open({
                type: 1,
                title: "新增控制方案",
                btnAlign: "c",
                offset: [offsetH],
                area: ['850px', '500px'],
                content: $("#controlDiv"),
                end: function (i) {
                    layer.close(i)
                    $("#editForm")[0].reset()
                    $("#controlDiv").hide();
                }
            });
        })

        form.on('submit(editSubmit)', function (obj) {
            var url = null;
            var data = obj.field
            if (data.outageStatus == 1) {
                if (isEmpty(data['interruptStartDate']))
                    return layer.msg("请选择断电开始时间!", {icon: 0})
                if (isEmpty(data['interruptEndDate']))
                    return layer.msg("请选择断电结束时间!", {icon: 0})
            } else {
                if (!isEmpty(data['interruptStartDate']) || !isEmpty(data['interruptEndDate']))
                    return layer.msg("欠费不断电不需要断电时间段", {icon: 0})
            }
            if (!isEmpty(controllerPlanId)) {
                data.controllerPlanId = controllerPlanId
                url = "controllerPlan/edit"
            } else
                url = "controllerPlan/save"
            postAjaxReq(url, data, function (res) {
                if (res.code == 1) {
                    layer.closeAll();
                    table.reload('controlList');
                    layer.msg("保存成功")
                    controllerPlanId = null
                } else {
                    layer.msg(res.msg)
                }
            })
        });

        $("#search").click(function () {
            var name = $("#name").val()
            table.reload('controlList', {
                page: {curr: 1},
                where: {name: name}
            })
        });
    });
});
$("#cancel").on("click", function () {
    layer.closeAll()
    $("#controlDiv").hide()
});


